package com.stress.testing.plugin.nosql.redis;

import com.stress.testing.common.matcher.ClassDescription;
import com.stress.testing.common.matcher.ClassMatcher;
import com.stress.testing.common.matcher.MethodDescription;
import com.stress.testing.common.matcher.MethodMatcher;
import com.stress.testing.common.plugin.AbstractPlugin;
import com.stress.testing.plugin.nosql.redis.util.JedisMethodsHolder;

import java.util.Collections;
import java.util.List;

/**
 * Jedis/JedisPool/ShardedJedis/ShardedJedisPool本质上都是使用Jedis
 * <p>
 * 注意：
 * BinaryJedis是Jedis的父类, BinaryJedisCluster是JedisCluster的父类，也是可以实例化的。
 * 个人认为使用比较少，忽略不拦截
 * 如果想拦截，在ClassMatcher#match中添加条件即可
 */
public class JedisPlugin extends AbstractPlugin {
    @Override
    public ClassMatcher getClassMatcher() {
        return new ClassMatcher() {
            @Override
            public boolean match(ClassDescription classDescription) {
                return "redis.clients.jedis.Jedis".equals(classDescription.getName())||
                        "redis.clients.jedis.JedisCluster".equals(classDescription.getName())||
                        "redis.clients.jedis.Pipeline".equals(classDescription.getName()) ||
                        "redis.clients.jedis.Transaction".equals(classDescription.getName()) ||
                        "redis.clients.jedis.ShardedJedisPipeline".equals(classDescription.getName());
            }

            @Override
            public List<MethodMatcher> getMethodMatchers() {
                return Collections.singletonList(new MethodMatcher() {
                    @Override
                    public boolean match(MethodDescription methodDescription) {
                        return JedisMethodsHolder.methodSet.contains(methodDescription.getName());
                    }

                    @Override
                    public String getInterceptor() {
                        return "com.stress.testing.plugin.nosql.redis.interceptor.JedisInterceptor";
                    }
                });
            }
        };
    }
}
